package controllers

import dao.{AnnoDao, BasicDao}
import implicits.Implicits._
import play.api.cache.AsyncCacheApi
import play.api.libs.json.Json
import play.api.mvc._
import tool.{FormTool, Tool}
import utils.Utils

import java.io.File
import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global


/**
 * Created by yz on 16/9/2021
 */
class MarkerController @Inject()(cc: ControllerComponents, cache: AsyncCacheApi)(
  implicit val basicDao: BasicDao,
  implicit val annoDao: AnnoDao,
) extends AbstractController(cc) {

  def toSsr = Action { implicit request =>
    Ok(views.html.marker.ssr())
  }

  def toGwas = Action { implicit request =>
    Ok(views.html.marker.gwas())
  }

  def getFileData = Action { implicit request =>
      val data = FormTool.fileNameForm.bindFromRequest().get
      val file = new File(Tool.dataDir, data.fileName)
      val lines= file.nameSuffix match {
        case "txt" =>file.txtLines.notEmptyLines
        case "csv" =>file.csvLines
        case "xlsx" =>file.xlsxLines()
      }
      val (map, array) = Utils.getInfoByLines(lines)
      val json = Json.obj("map" -> map, "array" -> array)
      Ok(json)
  }

}
